import React, {useCallback, useEffect, useState} from "react"
import { observerDashboardPageWrapper } from "./ObserverDashboardPageWrapper"
import OnlinePaginationOrderTable, {QueryOrderTableDataCondition} from "../OnlinePaginationOrderTable";
import {OrderSummary, queryOrders} from "../../apis/OrderApi";
import {mergePaginationAndPageable} from "../../utils/AntdTableUtils";
import {Pageable} from "../../utils/Page";
import {setEnvironmentData} from "node:worker_threads";
import DataPageLayout from "../DataPageLayout";

export interface DigitalOrderPageProps {

}

const DigitalOrderPage: React.FC<DigitalOrderPageProps> = () => {
    const [orders,setOrders] = useState<OrderSummary[]>([])
    const [condition,setCondition] = useState<QueryOrderTableDataCondition>()
    const [pageable, setPageable] = useState(new Pageable())
    const [totalElements, setTotalElements] = useState(0)
    const [loading,setLoading] = useState(false)
    const [startDate,setStartDate] = useState<string>()
    const [endDate,setEndDate] = useState<string>()
    const query = useCallback(async ()=>{
        let pageable = condition?.pagination?mergePaginationAndPageable(condition.pagination,new Pageable(),condition.sorter):new Pageable()
        setPageable(pageable)
        setLoading(loading)
        try {
            let response = await queryOrders({...condition,startDate,endDate}, pageable);
            setTotalElements(response.data.totalElements)
            setOrders(response.data.content)
        } finally {
            setLoading(false)
        }
    },[condition,startDate,endDate])

    useEffect(() => {
        query()
    }, [query]);
    return (
        <>
            <DataPageLayout leftActions={<>

            </>}>
                <OnlinePaginationOrderTable onConditionChange={setCondition}
                                            data={orders}
                                            pagination={{
                                                current: pageable.page,
                                                pageSize: pageable.size,
                                                total: totalElements,
                                            }}
                                            loading={loading}
                                            onDateRangeFilterChange={function (startDate?: string, endDate?: string): void {
                                                setStartDate(startDate)
                                                setEndDate(endDate)
                                            }}/>
            </DataPageLayout>
        </>
    )
}

export default observerDashboardPageWrapper(DigitalOrderPage, "digitalOrders", "数码订单")
